//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SecondOrderMixedDerivativeOp.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Operators/NinePointLinearOp.h>
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::CSecondOrderMixedDerivativeOp (UInt64 d0, UInt64 d1, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher) : CNinePointLinearOp(CSecondOrderMixedDerivativeOp::typeid)
{
    CFdmMesher^ _Cmesher;
    try
    {
#ifdef HANDLE
        _phSecondOrderMixedDerivativeOp = NULL;
#endif
        QuantLib::Size _d0 = (QuantLib::Size)ValueHelper::Convert (d0); //d
        QuantLib::Size _d1 = (QuantLib::Size)ValueHelper::Convert (d1); //d
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ppSecondOrderMixedDerivativeOp = new boost::shared_ptr<QuantLib::SecondOrderMixedDerivativeOp> (new QuantLib::SecondOrderMixedDerivativeOp ( _d0,  _d1,  _mesher ));
        SetNinePointLinearOp (boost::dynamic_pointer_cast<QuantLib::NinePointLinearOp> (*_ppSecondOrderMixedDerivativeOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::CSecondOrderMixedDerivativeOp (boost::shared_ptr<QuantLib::SecondOrderMixedDerivativeOp>& childNative, Object^ owner) : CNinePointLinearOp(CSecondOrderMixedDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondOrderMixedDerivativeOp = NULL;
#endif
	_ppSecondOrderMixedDerivativeOp = &childNative;
    _ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (boost::dynamic_pointer_cast<QuantLib::NinePointLinearOp> (*_ppSecondOrderMixedDerivativeOp));
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::CSecondOrderMixedDerivativeOp (QuantLib::SecondOrderMixedDerivativeOp& childNative, Object^ owner) : CNinePointLinearOp(CSecondOrderMixedDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondOrderMixedDerivativeOp = NULL;
#endif
	_ppSecondOrderMixedDerivativeOp = new boost::shared_ptr<QuantLib::SecondOrderMixedDerivativeOp> (&childNative);
    _ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (boost::dynamic_pointer_cast<QuantLib::NinePointLinearOp> (*_ppSecondOrderMixedDerivativeOp));
    _SecondOrderMixedDerivativeOpOwner = owner;
    _NinePointLinearOpOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::CSecondOrderMixedDerivativeOp (CSecondOrderMixedDerivativeOp^ copy) : CNinePointLinearOp(CSecondOrderMixedDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondOrderMixedDerivativeOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSecondOrderMixedDerivativeOp = new boost::shared_ptr<QuantLib::SecondOrderMixedDerivativeOp> (copy->GetShared());
        _ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (boost::dynamic_pointer_cast<QuantLib::NinePointLinearOp> (*_ppSecondOrderMixedDerivativeOp));
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::CSecondOrderMixedDerivativeOp (PLATFORM::Type^ t) : CNinePointLinearOp(CSecondOrderMixedDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondOrderMixedDerivativeOp = NULL;
#endif
	if (!t->IsSubclassOf(CSecondOrderMixedDerivativeOp::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::CSecondOrderMixedDerivativeOp (QuantLib::Handle<QuantLib::SecondOrderMixedDerivativeOp>& childNative, Object^ owner)  : CNinePointLinearOp(CSecondOrderMixedDerivativeOp::typeid)
{
	_phSecondOrderMixedDerivativeOp = &childNative;
	_ppSecondOrderMixedDerivativeOp = &static_cast<boost::shared_ptr<QuantLib::SecondOrderMixedDerivativeOp>>(childNative.currentLink());
    _ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (boost::dynamic_pointer_cast<QuantLib::NinePointLinearOp> (*_ppSecondOrderMixedDerivativeOp));
    _SecondOrderMixedDerivativeOpOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::CSecondOrderMixedDerivativeOp (QuantLib::Handle<QuantLib::SecondOrderMixedDerivativeOp> childNative)  : CNinePointLinearOp(CSecondOrderMixedDerivativeOp::typeid)
{
	_phSecondOrderMixedDerivativeOp = &childNative;
	_ppSecondOrderMixedDerivativeOp = &static_cast<boost::shared_ptr<QuantLib::SecondOrderMixedDerivativeOp>>(childNative.currentLink());
    _ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (boost::dynamic_pointer_cast<QuantLib::NinePointLinearOp> (*_ppSecondOrderMixedDerivativeOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::CSecondOrderMixedDerivativeOp (QuantLib::SecondOrderMixedDerivativeOp childNative)  : CNinePointLinearOp(CSecondOrderMixedDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondOrderMixedDerivativeOp = NULL;
#endif
	_ppSecondOrderMixedDerivativeOp = new boost::shared_ptr<QuantLib::SecondOrderMixedDerivativeOp> (new QuantLib::SecondOrderMixedDerivativeOp (childNative));
    _ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (boost::dynamic_pointer_cast<QuantLib::NinePointLinearOp> (*_ppSecondOrderMixedDerivativeOp));
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::~CSecondOrderMixedDerivativeOp ()
{
    if (_ppSecondOrderMixedDerivativeOp != NULL)
    {
	    delete _ppSecondOrderMixedDerivativeOp;
        _ppSecondOrderMixedDerivativeOp = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::!CSecondOrderMixedDerivativeOp ()
{
    if (_ppSecondOrderMixedDerivativeOp != NULL)
    {
	    delete _ppSecondOrderMixedDerivativeOp;
    }
}
QuantLib::SecondOrderMixedDerivativeOp& Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::GetReference ()
{
    if (_ppSecondOrderMixedDerivativeOp == NULL) throw REFNEW NativeNullException ();
	return **_ppSecondOrderMixedDerivativeOp;
}
boost::shared_ptr<QuantLib::SecondOrderMixedDerivativeOp>& Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::GetShared ()
{
    if (_ppSecondOrderMixedDerivativeOp == NULL) throw REFNEW NativeNullException ();
	return *_ppSecondOrderMixedDerivativeOp;
}
QuantLib::SecondOrderMixedDerivativeOp* Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::GetPointer ()
{
    if (_ppSecondOrderMixedDerivativeOp == NULL) throw REFNEW NativeNullException ();
	return &**_ppSecondOrderMixedDerivativeOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SecondOrderMixedDerivativeOp>& Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::GetHandle ()
{
	if (_phSecondOrderMixedDerivativeOp == NULL)
	{
		_phSecondOrderMixedDerivativeOp = new Handle<QuantLib::SecondOrderMixedDerivativeOp> (*_ppSecondOrderMixedDerivativeOp);
	}
	return *_phSecondOrderMixedDerivativeOp;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp::HasNative () 
{
	return (_ppSecondOrderMixedDerivativeOp != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Operators::ISecondOrderMixedDerivativeOp^ Cephei::QL::Methods::Finitedifferences::Operators::CSecondOrderMixedDerivativeOp_Factory::Create (UInt64 d0, UInt64 d1, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher)
{
    return REFNEW CSecondOrderMixedDerivativeOp ( d0,  d1,  mesher);
}
